
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>升级到Dart 2.14 · Dart编程指南</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="arcticfox">
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-pageview-count/plugin.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-theme-comscore/test.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="http://www.bczl.xyz/" target="_blank" class="custom-link">主页</a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="基础篇.html">
            
                <a href="基础篇.html">
            
                    
                    基础篇
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="环境搭建.html">
            
                <a href="环境搭建.html">
            
                    
                    环境搭建
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="简单语法.html">
            
                <a href="简单语法.html">
            
                    
                    简单语法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="函数.html">
            
                <a href="函数.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="进阶篇.html">
            
                <a href="进阶篇.html">
            
                    
                    进阶篇
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="面向对象.html">
            
                <a href="面向对象.html">
            
                    
                    面向对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="泛型.html">
            
                <a href="泛型.html">
            
                    
                    泛型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="枚举.html">
            
                <a href="枚举.html">
            
                    
                    枚举
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="库与导入.html">
            
                <a href="库与导入.html">
            
                    
                    库与导入
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="异步编程.html">
            
                <a href="异步编程.html">
            
                    
                    异步编程
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.5.1" data-path="异步编程之Future.html">
            
                <a href="异步编程之Future.html">
            
                    
                    Future
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5.2" data-path="异步编程之Stream.html">
            
                <a href="异步编程之Stream.html">
            
                    
                    Stream
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5.3" data-path="异步编程之Isolate.html">
            
                <a href="异步编程之Isolate.html">
            
                    
                    Isolate
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="异常处理.html">
            
                <a href="异常处理.html">
            
                    
                    异常处理与 Zone
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="生成器.html">
            
                <a href="生成器.html">
            
                    
                    生成器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="标准流与文件.html">
            
                <a href="标准流与文件.html">
            
                    
                    标准流与文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.9" data-path="运算符.html">
            
                <a href="运算符.html">
            
                    
                    附：运算符
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="常用标准库.html">
            
                <a href="常用标准库.html">
            
                    
                    常用标准库
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="字符串.html">
            
                <a href="字符串.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="集合.html">
            
                <a href="集合.html">
            
                    
                    集合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="URI处理.html">
            
                <a href="URI处理.html">
            
                    
                    URI处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="日期和时间.html">
            
                <a href="日期和时间.html">
            
                    
                    日期和时间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="环境及外部调用.html">
            
                <a href="环境及外部调用.html">
            
                    
                    环境及外部调用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="其他.html">
            
                <a href="其他.html">
            
                    
                    其他
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="实战篇.html">
            
                <a href="实战篇.html">
            
                    
                    实战篇
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="网络编程.html">
            
                <a href="网络编程.html">
            
                    
                    网络编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="爬虫实战.html">
            
                <a href="爬虫实战.html">
            
                    
                    爬虫实战
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="HTTP服务器.html">
            
                <a href="HTTP服务器.html">
            
                    
                    HTTP服务器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="WebSocket服务器.html">
            
                <a href="WebSocket服务器.html">
            
                    
                    WebSocket服务器
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" >
            
                <span>
            
                    
                    附录
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="1.6.1" data-path="dart2.14.html">
            
                <a href="dart2.14.html">
            
                    
                    升级到Dart 2.14
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="http://www.bczl.xyz/" target="blank" class="gitbook-link">
            编程之路
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >升级到Dart 2.14</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><li><span class="title-icon "></span><a href="#&#x5347;&#x7EA7;&#x5230;dart-214"><b></b>&#x5347;&#x7EA7;&#x5230;Dart 2.14</a></li><ul><li><span class="title-icon "></span><a href="#&#x7A7A;&#x5B89;&#x5168;"><b></b>&#x7A7A;&#x5B89;&#x5168;</a></li><ul><li><span class="title-icon "></span><a href="#&#x7C7B;&#x578B;&#x7CFB;&#x7EDF;&#x4E2D;&#x7684;&#x53EF;&#x7A7A;&#x6027;"><b></b>&#x7C7B;&#x578B;&#x7CFB;&#x7EDF;&#x4E2D;&#x7684;&#x53EF;&#x7A7A;&#x6027;</a></li><li><span class="title-icon "></span><a href="#&#x975E;&#x7A7A;&#x548C;&#x53EF;&#x7A7A;&#x7C7B;&#x578B;"><b></b>&#x975E;&#x7A7A;&#x548C;&#x53EF;&#x7A7A;&#x7C7B;&#x578B;</a></li><li><span class="title-icon "></span><a href="#&#x5B9E;&#x9645;&#x4F7F;&#x7528;"><b></b>&#x5B9E;&#x9645;&#x4F7F;&#x7528;</a></li></ul><li><span class="title-icon "></span><a href="#&#x7C7B;&#x578B;&#x522B;&#x540D;"><b></b>&#x7C7B;&#x578B;&#x522B;&#x540D;</a></li><li><span class="title-icon "></span><a href="#&#x5DE5;&#x5177;"><b></b>&#x5DE5;&#x5177;</a></li></ul></ul></div><a href="#&#x5347;&#x7EA7;&#x5230;dart-214" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a><!-- ex_nolevel -->
<h1 id="&#x5347;&#x7EA7;&#x5230;dart-214"><a name="&#x5347;&#x7EA7;&#x5230;dart-214" class="anchor-navigation-ex-anchor" href="#&#x5347;&#x7EA7;&#x5230;dart-214"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5347;&#x7EA7;&#x5230;Dart 2.14</h1>
<h2 id="&#x7A7A;&#x5B89;&#x5168;"><a name="&#x7A7A;&#x5B89;&#x5168;" class="anchor-navigation-ex-anchor" href="#&#x7A7A;&#x5B89;&#x5168;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x7A7A;&#x5B89;&#x5168;</h2>
<p>&#x968F;&#x7740;Flutter&#x7684;&#x4E0D;&#x65AD;&#x8FED;&#x4EE3;&#x66F4;&#x65B0;&#xFF0C;Dart&#x8BED;&#x8A00;&#x4E5F;&#x5728;&#x5FEB;&#x901F;&#x53D1;&#x5C55;&#x3002;&#x5176;&#x4E2D;&#x6700;&#x91CD;&#x8981;&#x7684;&#x53D8;&#x5316;&#xFF0C;&#x662F;Dart 2.12&#x6B63;&#x5F0F;&#x53D1;&#x5E03;&#x7684;&#x7A7A;&#x5B89;&#x5168;&#x7279;&#x6027;&#x3002;&#x7A7A;&#x5B89;&#x5168;&#x7279;&#x6027;&#x662F;&#x4E2A;&#x5206;&#x6C34;&#x5CAD;&#xFF0C;&#x5BFC;&#x81F4;Dart&#x8BED;&#x8A00;&#x9762;&#x8C8C;&#x53D1;&#x751F;&#x4E86;&#x5F88;&#x5927;&#x7684;&#x53D8;&#x5316;&#xFF0C;&#x8BB8;&#x591A;&#x5E93;&#x751A;&#x81F3;&#x76F4;&#x63A5;&#x653E;&#x5F03;&#x4E86;&#x5347;&#x7EA7;&#x7A7A;&#x5B89;&#x5168;&#xFF0C;&#x505C;&#x6B62;&#x4E86;&#x7EF4;&#x62A4;&#x3002;&#x8FD9;&#x91CC;&#x6211;&#x4EEC;&#x5347;&#x7EA7;Dart&#x7F16;&#x7A0B;&#x6307;&#x5357;&#x8BFE;&#x7A0B;&#xFF0C;&#x4E00;&#x8D77;&#x6765;&#x770B;&#x4E00;&#x4E0B;&#xFF0C;&#x4EC0;&#x4E48;&#x662F;&#x7A7A;&#x5B89;&#x5168;&#xFF0C;&#x600E;&#x4E48;&#x4F7F;&#x7528;&#x7A7A;&#x5B89;&#x5168;&#xFF1F;</p>
<h3 id="&#x7C7B;&#x578B;&#x7CFB;&#x7EDF;&#x4E2D;&#x7684;&#x53EF;&#x7A7A;&#x6027;"><a name="&#x7C7B;&#x578B;&#x7CFB;&#x7EDF;&#x4E2D;&#x7684;&#x53EF;&#x7A7A;&#x6027;" class="anchor-navigation-ex-anchor" href="#&#x7C7B;&#x578B;&#x7CFB;&#x7EDF;&#x4E2D;&#x7684;&#x53EF;&#x7A7A;&#x6027;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x7C7B;&#x578B;&#x7CFB;&#x7EDF;&#x4E2D;&#x7684;&#x53EF;&#x7A7A;&#x6027;</h3>
<p>&#x56E0;&#x4E3A;&#x4E00;&#x5207;&#x5747;&#x5EFA;&#x7ACB;&#x4E8E;&#x9759;&#x6001;&#x7C7B;&#x578B;&#x7CFB;&#x7EDF;&#x4E0A;&#xFF0C;&#x6240;&#x4EE5;&#x7A7A;&#x5B89;&#x5168;&#x4E5F;&#x59CB;&#x4E8E;&#x6B64;&#x5904;&#x3002;&#x4F60;&#x7684; Dart &#x7A0B;&#x5E8F;&#x4E2D;&#x5305;&#x542B;&#x4E86;&#x6574;&#x4E2A;&#x7C7B;&#x578B;&#x4E16;&#x754C;&#xFF1A;&#x57FA;&#x672C;&#x7C7B;&#x578B;&#xFF08;&#x5982; <code>int</code> &#x548C; <code>String</code>&#xFF09;&#x3001;&#x96C6;&#x5408;&#x7C7B;&#x578B;&#xFF08;&#x5982; <code>List</code>&#xFF09;&#x4EE5;&#x53CA;&#x4F60;&#x6240;&#x4F7F;&#x7528;&#x7684;&#x81EA;&#x5B9A;&#x4E49;&#x7C7B;&#x578B;&#x3002;&#x5728;&#x7A7A;&#x5B89;&#x5168;&#x63A8;&#x51FA;&#x4E4B;&#x524D;&#xFF0C;&#x9759;&#x6001;&#x7C7B;&#x578B;&#x7CFB;&#x7EDF;&#x5141;&#x8BB8;&#x6240;&#x6709;&#x7C7B;&#x578B;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x4E2D;&#x7684;&#x6BCF;&#x4E00;&#x5904;&#x90FD;&#x53EF;&#x4EE5;&#x6709; <code>null</code>&#x3002;</p>
<p>&#x4ECE;&#x7C7B;&#x578B;&#x7406;&#x8BBA;&#x7684;&#x89D2;&#x5EA6;&#x6765;&#x8BF4;&#xFF0C;<code>Null</code> &#x7C7B;&#x578B;&#x88AB;&#x770B;&#x4F5C;&#x662F;&#x6240;&#x6709;&#x7C7B;&#x578B;&#x7684;&#x5B50;&#x7C7B;&#xFF1B;</p>
<p><img src="https://dart.cn/null-safety/understanding-null-safety/hierarchy-before.png" alt=""></p>
<p>&#x7C7B;&#x578B;&#x4F1A;&#x5B9A;&#x4E49;&#x4E00;&#x4E9B;&#x64CD;&#x4F5C;&#x5BF9;&#x8C61;&#xFF0C;&#x5305;&#x62EC; getters&#x3001;setters&#x3001;&#x65B9;&#x6CD5;&#x548C;&#x64CD;&#x4F5C;&#x7B26;&#xFF0C;&#x5728;&#x8868;&#x8FBE;&#x5F0F;&#x4E2D;&#x4F7F;&#x7528;&#x3002;&#x5982;&#x679C;&#x662F; <code>List</code> &#x7C7B;&#x578B;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x5BF9;&#x5176;&#x8C03;&#x7528; <code>.add()</code> &#x6216; <code>[]</code>&#x3002;&#x5982;&#x679C;&#x662F; <code>int</code> &#x7C7B;&#x578B;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x5BF9;&#x5176;&#x8C03;&#x7528; <code>+</code>&#x3002;&#x4F46;&#x662F; <code>null</code> &#x5E76;&#x6CA1;&#x6709;&#x5B9A;&#x4E49;&#x4EFB;&#x4F55;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;&#x3002;&#x6240;&#x4EE5;&#x5F53; <code>null</code> &#x4F20;&#x9012;&#x81F3;&#x5176;&#x4ED6;&#x7C7B;&#x578B;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x65F6;&#xFF0C;&#x4EFB;&#x4F55;&#x64CD;&#x4F5C;&#x90FD;&#x6709;&#x53EF;&#x80FD;&#x5931;&#x8D25;&#x3002;&#x8FD9;&#x5C31;&#x662F;&#x7A7A;&#x5F15;&#x7528;&#x7684;&#x75C7;&#x7ED3;&#x6240;&#x5728;&#x2014;&#x2014;&#x6240;&#x6709;&#x9519;&#x8BEF;&#x90FD;&#x6765;&#x6E90;&#x4E8E;&#x5C1D;&#x8BD5;&#x5728; <code>null</code> &#x4E0A;&#x67E5;&#x627E;&#x4E00;&#x4E2A;&#x4E0D;&#x5B58;&#x5728;&#x7684;&#x65B9;&#x6CD5;&#x6216;&#x5C5E;&#x6027;&#x3002;</p>
<h3 id="&#x975E;&#x7A7A;&#x548C;&#x53EF;&#x7A7A;&#x7C7B;&#x578B;"><a name="&#x975E;&#x7A7A;&#x548C;&#x53EF;&#x7A7A;&#x7C7B;&#x578B;" class="anchor-navigation-ex-anchor" href="#&#x975E;&#x7A7A;&#x548C;&#x53EF;&#x7A7A;&#x7C7B;&#x578B;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x975E;&#x7A7A;&#x548C;&#x53EF;&#x7A7A;&#x7C7B;&#x578B;</h3>
<p>&#x7A7A;&#x5B89;&#x5168;&#x901A;&#x8FC7;&#x4FEE;&#x6539;&#x4E86;&#x7C7B;&#x578B;&#x7684;&#x5C42;&#x7EA7;&#x7ED3;&#x6784;&#xFF0C;&#x4ECE;&#x6839;&#x6E90;&#x4E0A;&#x89E3;&#x51B3;&#x4E86;&#x8FD9;&#x4E2A;&#x95EE;&#x9898;&#x3002; <code>Null</code> &#x7C7B;&#x578B;&#x4ECD;&#x7136;&#x5B58;&#x5728;&#xFF0C;&#x4F46;&#x5B83;&#x4E0D;&#x518D;&#x662F;&#x6240;&#x6709;&#x7C7B;&#x578B;&#x7684;&#x5B50;&#x7C7B;&#x3002;&#x73B0;&#x5728;&#x7684;&#x7C7B;&#x578B;&#x5C42;&#x7EA7;&#x770B;&#x8D77;&#x6765;&#x662F;&#x8FD9;&#x6837;&#x7684;&#xFF1A;</p>
<p><img src="https://dart.cn/null-safety/understanding-null-safety/hierarchy-after.png" alt=""></p>
<p>&#x65E2;&#x7136; <code>Null</code> &#x5DF2;&#x4E0D;&#x518D;&#x88AB;&#x770B;&#x4F5C;&#x6240;&#x6709;&#x7C7B;&#x578B;&#x7684;&#x5B50;&#x7C7B;&#xFF0C;&#x90A3;&#x4E48;&#x9664;&#x4E86;&#x7279;&#x6B8A;&#x7684; <code>Null</code> &#x7C7B;&#x578B;&#x5141;&#x8BB8;&#x4F20;&#x9012; <code>null</code> &#x503C;&#xFF0C;&#x5176;&#x4ED6;&#x7C7B;&#x578B;&#x5747;&#x4E0D;&#x5141;&#x8BB8;&#x3002;&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x5C06;&#x6240;&#x6709;&#x7684;&#x7C7B;&#x578B;&#x8BBE;&#x7F6E;&#x4E3A; <strong>&#x9ED8;&#x8BA4;&#x4E0D;&#x53EF;&#x7A7A;</strong> &#x7684;&#x7C7B;&#x578B;&#x3002;&#x5982;&#x679C;&#x4F60;&#x7684;&#x53D8;&#x91CF;&#x662F; <code>String</code> &#x7C7B;&#x578B;&#xFF0C;&#x5B83;&#x5FC5;&#x987B;&#x5305;&#x542B; <strong>&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;</strong>&#x3002;&#x8FD9;&#x6837;&#x4E00;&#x6765;&#xFF0C;&#x6211;&#x4EEC;&#x5C31;&#x4FEE;&#x590D;&#x4E86;&#x6240;&#x6709;&#x7684;&#x7A7A;&#x5F15;&#x7528;&#x9519;&#x8BEF;&#x3002;</p>
<pre><code class="lang-dart"><span class="hljs-comment">// &#x4F7F;&#x7528;&#x7A7A;&#x5B89;&#x5168;</span>
makeCoffee(<span class="hljs-built_in">String</span> coffee, [<span class="hljs-built_in">String</span>? dairy]) {
  <span class="hljs-keyword">if</span> (dairy != <span class="hljs-keyword">null</span>) {
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&apos;$coffee with $dairy&apos;</span>);
  } <span class="hljs-keyword">else</span> {
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&apos;Black $coffee&apos;</span>);
  }
}
</code></pre>
<p>&#x6B64;&#x5904;&#x6211;&#x4EEC;&#x5E0C;&#x671B; <code>dairy</code> &#x53C2;&#x6570;&#x80FD;&#x4F20;&#x5165;&#x4EFB;&#x610F;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x6216;&#x8005;&#x4E00;&#x4E2A; <code>null</code> &#x503C;&#x3002;&#x4E3A;&#x4E86;&#x8868;&#x8FBE;&#x6211;&#x4EEC;&#x7684;&#x60F3;&#x6CD5;&#xFF0C;&#x6211;&#x4EEC;&#x5728;&#x539F;&#x6709;&#x7C7B;&#x578B; String &#x7684;&#x5C3E;&#x90E8;&#x52A0;&#x4E0A; <code>?</code> &#x4F7F;&#x5F97; <code>dairy</code> &#x6210;&#x4E3A;&#x53EF;&#x7A7A;&#x7684;&#x7C7B;&#x578B;&#x3002;&#x672C;&#x8D28;&#x4E0A;&#xFF0C;&#x8FD9;&#x548C;&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E2A;&#x539F;&#x6709;&#x7C7B;&#x578B;&#x52A0; Null &#x7684; &#x7EC4;&#x5408;&#x7C7B;&#x578B; &#x6CA1;&#x6709;&#x4EC0;&#x4E48;&#x533A;&#x522B;&#x3002;<code>String?</code> &#x8868;&#x8FBE;&#x7684;&#x610F;&#x601D;&#x76F8;&#x5F53;&#x4E8E;&#x662F; <code>String|Null</code> &#x7684;&#x7F29;&#x5199;&#x3002;</p>
<p><img src="https://gitee.com/arcticfox1919/ImageHosting/raw/master/img/bifurcate.png" alt=""></p>
<p>&#x8981;&#x8C28;&#x614E;&#x4F7F;&#x7528;&#x5141;&#x8BB8;&#x7A7A;&#x503C;&#x7684;&#x60C5;&#x51B5;&#xFF0C;&#x907F;&#x514D;&#x5982;&#x4E0B;&#x7684;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-dart">bad(<span class="hljs-built_in">String</span>? maybeString) {
  <span class="hljs-built_in">print</span>(maybeString.length);
}

main() {
  bad(<span class="hljs-keyword">null</span>);
}
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x4EE3;&#x7801;&#x5C06;&#x6BEB;&#x65E0;&#x7591;&#x95EE;&#x5730;&#x5D29;&#x6E83;&#x3002;</p>
<p>&#x5173;&#x4E8E;Dart&#x65B0;&#x7279;&#x6027;&#x7A7A;&#x5B89;&#x5168;&#x66F4;&#x591A;&#x8BE6;&#x7EC6;&#x6DF1;&#x5165;&#x7684;&#x7406;&#x89E3;&#xFF0C;&#x8BF7;&#x8F6C;&#x5230;Dart&#x5B98;&#x65B9;&#x6587;&#x6863;<a href="https://dart.cn/null-safety/understanding-null-safety" title="&#x300A;&#x6DF1;&#x5165;&#x7406;&#x89E3;&#x7A7A;&#x5B89;&#x5168;&#x300B;" target="_blank">&#x300A;&#x6DF1;&#x5165;&#x7406;&#x89E3;&#x7A7A;&#x5B89;&#x5168;&#x300B;</a></p>
<p>&#x9700;&#x8981;&#x6CE8;&#x610F;&#x7684;&#x662F;&#xFF0C;&#x5982;&#x679C;&#x4F7F;&#x7528;Dart 2.12&#x53CA;&#x4EE5;&#x4E0A;&#x7248;&#x672C;&#xFF0C;&#x4E0D;&#x4EC5;&#x6211;&#x4EEC;&#x81EA;&#x5DF1;&#x5199;&#x7684;&#x4EE3;&#x7801;&#x9700;&#x8981;&#x652F;&#x6301;&#x4E0D;&#x517C;&#x5BB9;&#x7684;&#x7A7A;&#x5B89;&#x5168;&#x65B0;&#x7279;&#x6027;&#xFF0C;&#x6240;&#x6709;&#x4F9D;&#x8D56;&#x7684;&#x7B2C;&#x4E09;&#x65B9;&#x5E93;&#x4E5F;&#x5FC5;&#x987B;&#x8981;&#x652F;&#x6301;&#x7A7A;&#x5B89;&#x5168;&#x7279;&#x6027;&#x3002;</p>
<h3 id="&#x5B9E;&#x9645;&#x4F7F;&#x7528;"><a name="&#x5B9E;&#x9645;&#x4F7F;&#x7528;" class="anchor-navigation-ex-anchor" href="#&#x5B9E;&#x9645;&#x4F7F;&#x7528;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5B9E;&#x9645;&#x4F7F;&#x7528;</h3>
<h4 id="&#x53D8;&#x91CF;&#x4E2D;&#x7684;&#x7A7A;&#x5B89;&#x5168;"><a name="&#x53D8;&#x91CF;&#x4E2D;&#x7684;&#x7A7A;&#x5B89;&#x5168;" class="anchor-navigation-ex-anchor" href="#&#x53D8;&#x91CF;&#x4E2D;&#x7684;&#x7A7A;&#x5B89;&#x5168;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x53D8;&#x91CF;&#x4E2D;&#x7684;&#x7A7A;&#x5B89;&#x5168;</h4>
<pre><code class="lang-dart"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">A</span> </span>{
  <span class="hljs-built_in">int</span> a;
  <span class="hljs-built_in">String</span> s;
  <span class="hljs-comment">// late&#x5EF6;&#x8FDF;&#x521D;&#x59CB;&#x5316;</span>
  late <span class="hljs-built_in">bool</span> b;

  <span class="hljs-comment">// &#x52A0;?&#x4FEE;&#x9970;&#xFF0C;label&#x503C;&#x53EF;&#x4EE5;&#x4E3A;null</span>
  <span class="hljs-built_in">String</span>? label;

  A(<span class="hljs-keyword">this</span>.a, <span class="hljs-keyword">this</span>.s) {
    <span class="hljs-keyword">if</span> (a &gt; <span class="hljs-number">0</span>)
      b = <span class="hljs-keyword">true</span>;
    <span class="hljs-keyword">else</span>
      b = <span class="hljs-keyword">false</span>;
  }
}
</code></pre>
<h4 id="&#x51FD;&#x6570;&#x4E2D;&#x7684;&#x7A7A;&#x5B89;&#x5168;"><a name="&#x51FD;&#x6570;&#x4E2D;&#x7684;&#x7A7A;&#x5B89;&#x5168;" class="anchor-navigation-ex-anchor" href="#&#x51FD;&#x6570;&#x4E2D;&#x7684;&#x7A7A;&#x5B89;&#x5168;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x51FD;&#x6570;&#x4E2D;&#x7684;&#x7A7A;&#x5B89;&#x5168;</h4>
<pre><code class="lang-dart"><span class="hljs-comment">// name&#x4E3A;&#x7A7A;&#x5B89;&#x5168;&#xFF0C;&#x4F7F;&#x7528;&#x65F6;&#x65E0;&#x9700;&#x5224;&#x7A7A;</span>
<span class="hljs-keyword">void</span> greet(<span class="hljs-built_in">String</span> name) {
    <span class="hljs-built_in">print</span>(name.length);
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;hello,&quot;</span> + name);
}

<span class="hljs-comment">// &#x53C2;&#x6570;&#x53EF;&#x4E3A;&#x7A7A;&#x65B9;&#x6CD5;&#x4E2D;&#x7684;&#x4F7F;&#x7528;</span>
<span class="hljs-comment">// &#x547D;&#x540D;&#x53C2;&#x6570;&#x4E2D;&#x589E;&#x52A0;&#x4E86;required&#x5173;&#x952E;&#x5B57;</span>
<span class="hljs-keyword">void</span> func({required <span class="hljs-built_in">String</span>? abc, <span class="hljs-built_in">double</span>? d}) {}

<span class="hljs-comment">// &#x8FD4;&#x56DE;&#x503C;&#x4E3A;&#x975E;&#x7A7A;&#x7C7B;&#x578B;&#xFF0C;&#x4E0D;&#x80FD;&#x8FD4;&#x56DE;null</span>
<span class="hljs-built_in">String</span> getName() {
   <span class="hljs-comment">// return null;</span>
   <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;&apos;</span>;
}

<span class="hljs-comment">// &#x8981;&#x8FD4;&#x56DE;&#x5305;&#x542B;null&#x7C7B;&#x578B;&#xFF0C;&#x4E5F;&#x9700;&#x8981;&#x52A0;?</span>
<span class="hljs-built_in">String</span>? user() {
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;
}
</code></pre>
<h4 id="&#x6CDB;&#x578B;&#x4E2D;&#x7684;&#x7A7A;&#x5B89;&#x5168;"><a name="&#x6CDB;&#x578B;&#x4E2D;&#x7684;&#x7A7A;&#x5B89;&#x5168;" class="anchor-navigation-ex-anchor" href="#&#x6CDB;&#x578B;&#x4E2D;&#x7684;&#x7A7A;&#x5B89;&#x5168;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x6CDB;&#x578B;&#x4E2D;&#x7684;&#x7A7A;&#x5B89;&#x5168;</h4>
<pre><code class="lang-dart"><span class="hljs-comment">// list1&#x53D8;&#x91CF;&#x4E0D;&#x4E3A;&#x7A7A;&#xFF0C;&#x4F46;&#x662F;&#x5217;&#x8868;&#x4E2D;&#x5143;&#x7D20;&#x53EF;&#x4E3A;null</span>
<span class="hljs-built_in">List</span>&lt;<span class="hljs-built_in">String</span>?&gt; list1;
<span class="hljs-comment">// list2&#x53D8;&#x91CF;&#x548C;&#x5217;&#x8868;&#x4E2D;&#x5143;&#x7D20;&#x90FD;&#x53EF;&#x4E3A;null</span>
<span class="hljs-built_in">List</span>&lt;<span class="hljs-built_in">String</span>?&gt;? list2;
<span class="hljs-comment">// list3&#x53D8;&#x91CF;&#x548C;&#x5217;&#x8868;&#x4E2D;&#x5143;&#x7D20;&#x90FD;&#x4E0D;&#x80FD;&#x4E3A;null</span>
<span class="hljs-built_in">List</span>&lt;<span class="hljs-built_in">String</span>&gt; list3;
</code></pre>
<h4 id="&#x5176;&#x4ED6;"><a name="&#x5176;&#x4ED6;" class="anchor-navigation-ex-anchor" href="#&#x5176;&#x4ED6;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5176;&#x4ED6;</h4>
<p>&#x4E0D;&#x80FD;&#x4F7F;&#x7528;<code>List</code>&#x9ED8;&#x8BA4;&#x7684;&#x6784;&#x9020;&#x65B9;&#x6CD5;<code>List()</code></p>
<pre><code class="lang-dart"><span class="hljs-comment">// &#x4F7F;&#x7528;filled&#x65B9;&#x6CD5;&#x66FF;&#x4EE3;</span>
<span class="hljs-built_in">List</span>&lt;<span class="hljs-built_in">int</span>&gt; list = <span class="hljs-built_in">List</span>.filled(<span class="hljs-number">19</span>, <span class="hljs-number">0</span>);
</code></pre>
<pre><code class="lang-dart"><span class="hljs-comment">// &#x9759;&#x6001;&#x4EE3;&#x7801;&#x5206;&#x6790;&#x4E0D;&#x51FA;&#x6765;&#xFF0C;&#x4E3B;&#x52A8;&#x7ED9;&#x7F16;&#x8BD1;&#x5668;&#x4E00;&#x4E2A;&#x63D0;&#x793A;</span>
<span class="hljs-comment">// 1.&#x4F7F;&#x7528;&#x201C;!&#x201D;&#x544A;&#x8BC9;&#x7F16;&#x8BD1;&#x5668;&#xFF0C;&#x53D8;&#x91CF;&#x4E0D;&#x4E3A;&#x7A7A;&#xFF0C;&#x6D88;&#x9664;&#x7F16;&#x8BD1;&#x9519;&#x8BEF;</span>
<span class="hljs-comment">// 2.&#x4F7F;&#x7528;&#x5C40;&#x90E8;&#x53D8;&#x91CF;</span>
<span class="hljs-keyword">if</span> (list != <span class="hljs-keyword">null</span>) {
    <span class="hljs-keyword">if</span> (list![<span class="hljs-number">0</span>] != <span class="hljs-keyword">null</span>) {}
}
</code></pre>
<p>&#x4F7F;&#x7528;&#x5E03;&#x5C40;&#x53D8;&#x91CF;&#x6D88;&#x9664;&#x7F16;&#x8BD1;&#x9519;&#x8BEF;</p>
<pre><code class="lang-dart">  A? a;
  <span class="hljs-keyword">if</span> (a != <span class="hljs-keyword">null</span>) {
    <span class="hljs-keyword">var</span> lab = a.label;
    <span class="hljs-keyword">if</span> (lab != <span class="hljs-keyword">null</span>) {
      <span class="hljs-built_in">print</span>(lab.length);
    }
  }
</code></pre>
<h2 id="&#x7C7B;&#x578B;&#x522B;&#x540D;"><a name="&#x7C7B;&#x578B;&#x522B;&#x540D;" class="anchor-navigation-ex-anchor" href="#&#x7C7B;&#x578B;&#x522B;&#x540D;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x7C7B;&#x578B;&#x522B;&#x540D;</h2>
<p>Dart 2.13 &#x7248;&#x63A8;&#x51FA;&#x4E86;&#x4E00;&#x4E2A;&#x65B0;&#x7279;&#x6027;&#xFF0C;&#x7C7B;&#x578B;&#x522B;&#x540D;&#x3002;</p>
<pre><code class="lang-dart"><span class="hljs-keyword">typedef</span> Integer = <span class="hljs-built_in">int</span>;

<span class="hljs-keyword">void</span> main() {
  <span class="hljs-built_in">print</span>(<span class="hljs-built_in">int</span> == Integer); <span class="hljs-comment">// true</span>
}
</code></pre>
<p>&#x5E38;&#x89C1;&#x7684;&#x7528;&#x6CD5;&#x662F;&#x7ED9;&#x67D0;&#x7C7B;&#x578B;&#x6307;&#x5B9A;&#x4E00;&#x4E2A;&#x66F4;&#x77ED;&#x6216;&#x66F4;&#x5177;&#x63CF;&#x8FF0;&#x6027;&#x7684;&#x540D;&#x79F0;&#xFF0C;&#x4EE5;&#x4FBF;&#x4EE3;&#x7801;&#x66F4;&#x6613;&#x4E8E;&#x7406;&#x89E3;&#x548C;&#x7EF4;&#x62A4;&#x3002;&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-dart"><span class="hljs-comment">// &#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x7C7B;&#x578B;&#x522B;&#x540D;</span>
<span class="hljs-keyword">typedef</span> Json = <span class="hljs-built_in">Map</span>&lt;<span class="hljs-built_in">String</span>, <span class="hljs-keyword">dynamic</span>&gt;;

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span> </span>{
  <span class="hljs-keyword">final</span> <span class="hljs-built_in">String</span> name;
  <span class="hljs-keyword">final</span> <span class="hljs-built_in">int</span> age;

  User.fromJson(Json json) : 
    name = json[<span class="hljs-string">&apos;name&apos;</span>],
    age = json[<span class="hljs-string">&apos;age&apos;</span>];

  Json <span class="hljs-keyword">get</span> json =&gt; {
    <span class="hljs-string">&apos;name&apos;</span>: name, 
    <span class="hljs-string">&apos;age&apos;</span>: age,
  };
}
</code></pre>
<h2 id="&#x5DE5;&#x5177;"><a name="&#x5DE5;&#x5177;" class="anchor-navigation-ex-anchor" href="#&#x5DE5;&#x5177;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5DE5;&#x5177;</h2>
<p>&#x5E9F;&#x5F03;<code>stagehand</code>&#x3001;<code>dartfmt</code>&#x548C;<code>dart2native</code>&#x547D;&#x4EE4;&#xFF0C;&#x7EDF;&#x4E00;&#x4F7F;&#x7528;<code>dart</code>&#x547D;&#x4EE4;&#x3002;</p>
<p>&#x4E4B;&#x524D;&#x6211;&#x4EEC;&#x4ECB;&#x7ECD;&#x4F7F;&#x7528;<code>pub global run stagehand console-full</code>&#x547D;&#x4EE4;&#x751F;&#x6210;Dart&#x547D;&#x4EE4;&#x884C;&#x9879;&#x76EE;&#xFF0C;&#x73B0;&#x5728;&#x4F7F;&#x7528;&#x4E0B;&#x9762;&#x7684;&#x547D;&#x4EE4;&#x66FF;&#x4EE3;&#xFF1A;</p>
<pre><code class="lang-shell"># &#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x547D;&#x4EE4;&#x884C;&#x6A21;&#x7248;&#x5DE5;&#x7A0B;
dart create -t console-full my_app

cd my_app
# &#x8FD0;&#x884C;&#x9879;&#x76EE;
dart run bin/my_app.dart
</code></pre>
<p>&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>dart -h</code>&#x67E5;&#x770B;&#x5176;&#x4ED6;&#x652F;&#x6301;&#x7684;&#x53C2;&#x6570;&#x3002;</p>
<p>&#x4EE5;&#x524D;&#x4F7F;&#x7528;<code>dart2native</code>&#x7F16;&#x8BD1;&#xFF0C;&#x73B0;&#x5728;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>dart compile</code>&#x547D;&#x4EE4;&#x5C06; Dart &#x7A0B;&#x5E8F;&#x7F16;&#x8BD1;&#x5230;&#x76EE;&#x6807;&#x5E73;&#x53F0;&#xFF1A;</p>
<pre><code class="lang-shell"># &#x751F;&#x6210;&#x72EC;&#x7ACB;&#x7684;&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;
dart compile exe bin/myapp.dart
# &#x751F;&#x6210; AOT &#x5FEB;&#x7167;
dart compile aot-snapshot bin/myapp.dart
# &#x751F;&#x6210;JIT&#x5FEB;&#x7167;
dart compile jit-snapshot bin/myapp.dart
# &#x751F;&#x6210;&#x5185;&#x6838;&#x5FEB;&#x7167;(&#x4E00;&#x79CD;&#x53EF;&#x79FB;&#x690D;&#x7684; &#x4E8C;&#x8FDB;&#x5236;&#x5FEB;&#x7167;)
dart compile kernel bin/myapp.dart
# &#x751F;&#x6210;JavaScript
dart compile js bin/myapp.dart
</code></pre>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; Arcticfox 2020 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x6587;&#x6863;&#x4FEE;&#x8BA2;&#x4E8E;&#xFF1A;
2021-10-18 22:51:20
</span></footer>
<script>console.log("plugin-popup....");document.onclick = function(e){ e.target.tagName === "IMG" && window.open(e.target.src,e.target.src)}</script><style>img{cursor:pointer}</style>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"升级到Dart 2.14","level":"1.6.1","depth":2,"previous":{"title":"附录","level":"1.6","depth":1,"ref":"","articles":[{"title":"升级到Dart 2.14","level":"1.6.1","depth":2,"path":"dart2.14.md","ref":"dart2.14.md","articles":[]}]},"dir":"ltr"},"config":{"plugins":["anchor-navigation-ex","expandable-chapters-small","-lunr","-search","search-pro","-sharing","github","expandable-chapters","pageview-count","theme-comscore","tbfed-pagefooter","popup","custom-favicon"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy Arcticfox 2020","modify_label":"文档修订于：","modify_format":"YYYY-MM-DD HH:mm:ss"},"github":{"url":"https://github.com/arcticfox1919"},"search-pro":{},"popup":{},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"floatIcon":"fa fa-navicon","level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":true,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":true,"showLevel":true},"favicon":"logo.ico","theme-comscore":{},"pageview-count":{},"custom-favicon":{},"expandable-chapters-small":{},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"expandable-chapters":{}},"theme":"default","author":"arcticfox","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Dart编程指南","language":"zh-hans","links":{"sidebar":{"主页":"http://www.bczl.xyz/"}},"gitbook":"*"},"file":{"path":"dart2.14.md","mtime":"2021-10-18T14:51:20.639Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-10-18T14:58:03.414Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-pageview-count/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-theme-comscore/test.js"></script>
        
    

    </body>
</html>

